package com.kcht.sylyg08.service.model;

import com.kcht.sylyg08.service.TagRecvBufferHandler;

import java.util.*;

/**
 * 标签计时，过时则为出工位
 */
public class TagInfoTimer {
    private TagInfo tagInfo;
    private Long FilterSwitch;
    private Timer timer;
    private TagRecvBufferHandler bufferHandler;

    /*
     * 取消计时器任务
     * 设置此对象的tagInfo
     * 开始执行任务
     * */
    public TagInfoTimer(TagInfo tagInfo, Long FilterSwitch, TagRecvBufferHandler bufferHandler) {
        this.tagInfo = tagInfo;
        this.FilterSwitch = FilterSwitch;
        this.bufferHandler = bufferHandler;
        this.timerStart();
    }

    /*计时器*/
    private void timerStart() {
        timer = new Timer();
        try {
            this.timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    bufferHandler.leave(tagInfo);
                    timer.cancel();
                    timer.purge();
                }
            }, FilterSwitch);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    public void updateTimer(TagInfo tagInfo) {
        timer.cancel();
        timer.purge();
        this.tagInfo = tagInfo;
        this.timerStart();
    }

    public void cancelTimer() {
        this.timer.cancel();
        this.timer.purge();
    }

    public TagInfo getTagInfo() {
        return tagInfo;
    }
}
